<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\NetCourseStoreRequest;
use App\Models\CourseColumn;
use App\Models\LearnVideoSection;
use App\Models\NetCourse;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class NetCourseController extends AdminBaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return JsonResponse|object
     */
    public function index()
    {
        $list = NetCourse::filter(\request()->all())->orderByRaw('id desc')->paginate(\request('perPage', 10));
        return $this->restful($list);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param NetCourseStoreRequest $request
     * @return JsonResponse|object
     */
    public function store(NetCourseStoreRequest $request)
    {
        $params = $request->validated();
        if (!empty($params['province'])) {
            $params['province'] = json_encode($params['province'], JSON_UNESCAPED_UNICODE);
        }
        if (empty($params['section_count'])) {
            // 计算课程数
            $courseColumn = CourseColumn::whereIn('id', $params['course_column_ids'])->pluck('resource')->toArray();
            $learnVideoIds = [];
            foreach ($courseColumn as $item) {
                if ($item != null && !empty($item['learn_videos'])) {
                    $learnVideoIds = array_merge($learnVideoIds, $item['learn_videos']);
                }
            }
            $selectCount = LearnVideoSection::whereIn('learn_video_id', $learnVideoIds)->count();
            $params['section_count'] = $selectCount;
        }

        $model = NetCourse::create($params);

        return $this->restful($model);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param NetCourseStoreRequest $request
     * @param int $id
     * @return JsonResponse|object
     */
    public function update(NetCourseStoreRequest $request, int $id)
    {
        $params = $request->validated();
        if (!empty($params['province'])) {
            $params['province'] = json_encode($params['province'], JSON_UNESCAPED_UNICODE);
        }
        if (empty($params['section_count'])) {
            // 计算课程数
            $courseColumn = CourseColumn::whereIn('id', $params['course_column_ids'])->pluck('resource')->toArray();
            $learnVideoIds = [];
            foreach ($courseColumn as $item) {
                if ($item != null && !empty($item['learn_videos'])) {
                    $learnVideoIds = array_merge($learnVideoIds, $item['learn_videos']);
                }
            }
            $selectCount = LearnVideoSection::whereIn('learn_video_id', $learnVideoIds)->count();
            $params['section_count'] = $selectCount;
        }
        $model = NetCourse::where('id', $id)->update($params);
        return $this->restful($model);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return JsonResponse|object
     */
    public function destroy($id)
    {
        $model = NetCourse::where('id', $id)->delete();

        return $this->restful($model);
    }
}
